<?php
    require_once('config.php');
    $pdo = new PDO("mysql:host=".mysqlAddress.";dbname=".mysqlDB,mysqlUser,mysqlPassword);
    $pdo->query("set names utf8");

    $sql = "select ".columnSaveUrl.",".columnType.",".columnName." from ".mysqlTable;
    $sql1 = "select count(*) from ".mysqlTable;
    if(isset($_POST['url'])){
        $sql .= " where ".columnSaveUrl."='".$_POST['url']."'";
        $sql1 .= " where ".columnSaveUrl."='".$_POST['url']."'";
    }
    $sql .= " order by type='image' asc";
    if(isset($_POST['page']) && isset($_POST['limit'])){
        $sql .= " limit ".(($_POST['page']-1)*$_POST['limit'])." , ".$_POST['limit']."";
    }
    // $res = $pdo->query("select ".columnSaveUrl.",".columnType.",".columnName." from ".mysqlTable." order by type='image' asc");
    $res = $pdo->query($sql);
    $re = $pdo->query($sql1);
    $total = $re->fetch()['count(*)'];
    $data = array();
    while($row = $res->fetch()){
        $data[] = ($row);
    }
    $data['lastPage'] = ceil($total/$_POST['limit']);
    $data['currentPage'] = $_POST['page'];
    if(!$data['lastPage']){
        $data['lastPage'] = 1;
        $data['currentPage'] = 1;
    }
    if($data['currentPage'] > $data['lastPage']){
        $data['currentPage'] = $data['lastPage'];
    }
    echo json_encode($data);
